package com.diary.database;

import com.diary.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CreateTable {

    public static void create(String username) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        String tablename = username;

        try {
            connection = DBUtils.getConnection();

            String sql = "CREATE TABLE `" + username + "` (" +
                    "    id INT AUTO_INCREMENT PRIMARY KEY," +
                    "    diary_title VARCHAR(50) NOT NULL," +
                    "    diary_body VARCHAR(15000)," +
                    "    data_time VARCHAR(50) NOT NULL," +
                    "    accepted VARCHAR(50) NOT NULL," +
                    "    life INT NOT NULL DEFAULT 0" +
                    ")";
            preparedStatement = connection.prepareStatement(sql);

//            preparedStatement.setString(1,username);


            boolean target = preparedStatement.execute();
            // 这里由于是进行新建数据表，不会影响到任何行，所以返回的内容总是false，不能使用这个返回值

//            return true;
//            return resultSet.next();


        } catch (SQLException e) {
            e.printStackTrace();
//            return false;
        }
        finally {
            DBUtils.close(resultSet,preparedStatement, connection);
        }
    }
}
